package com.diary.database;

import com.diary.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CreateUser {
    public static boolean add(String user, String password) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtils.getConnection();

            String sql = "INSERT INTO users (user, password) VALUES (?, ?);";

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user);
            preparedStatement.setString(2,password);

            int ans = preparedStatement.executeUpdate();
            //其实获取了也没啥用，最多是一条受到影响
            if (ans == 0){
                return false;
            }

            return true;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }
}
